package hot100;

import java.util.HashMap;
import java.util.Map;

public class Lc138 {
    public static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

    public Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        Node cur = head;
        Node dummy = new Node(0);
        Node temp = dummy;
        while (cur != null) {
            temp.next = new Node(cur.val);
            map.put(cur, temp.next);
            temp = temp.next;
            cur = cur.next;
        }
        Node cur1 = head;
        Node cur2 = dummy.next;
        while (cur1 != null) {
            if (cur1.random != null) {
                cur2.random = map.get(cur1.random);
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return dummy.next;
    }
}
